import axios from 'axios';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
axios.defaults.headers.get['Content-Type'] = 'application/x-www-form-urlencoded';

let hulkurl = 'http://127.0.0.1:8088';
// let hulkurl = 'http://192.168.138.129:8088';
let base = '';

//登录请求
export const requestLogin = params => {
    console.log(params);
    return axios.post(`${hulkurl}/api/login`, params).then(res => res.data); };

//根据token获取用户权限
export const getUserPermission = token => {
    // console.log(params);
    return axios.get(`${hulkurl}/api/userinfo`,
        {
            headers: {
                'token': token
            }
        }
        ).then(res => res.data); };


//用户管理
export const getHulkUsers = _ => { return axios.get(`${hulkurl}/api/sysconfig/users`)};




export const deleteUser = params => {
    return axios.post(`${hulkurl}/api/sysconfig/deleteUser`, params).then(res => res.data); };

export const resetPass = params => {
    console.log(params);
    return axios.post(`${hulkurl}/api/sysconfig/resetPassword`, params).then(res => res.data); };

export const addUser = params => {
    console.log(params);
    return axios.post(`${hulkurl}/api/sysconfig/addUser`, params).then(res => res.data); };

//服务器管理
export const getAssetList = _ => { return axios.get(`${hulkurl}/api/server/assets`)};
export const getDbList = _ => { return axios.get(`${hulkurl}/api/server/dbList`)};
export const getDbLists = params => { return axios.get(`${hulkurl}/api/server/instanceDbList?instance=` + params.instance)};
export const getSqlJobList = params => { return axios.get(`${hulkurl}/api/server/sqlJobList?dbname=` + params.dbname,{
    headers: {
        'token': params.token
    }
});};

export const rejectDbJob = params => {
    return axios.post(`${hulkurl}/api/server/rejectDbJob`, params).then(res => res.data); };

export const addSqlJob = params => {
    return axios.post(`${hulkurl}/api/server/addDbJob`, params).then(res => res.data); };
export const doSqlJob = params => {
    return axios.post(`${hulkurl}/api/server/execDbJob`, params).then(res => res.data); };
export const sqlJobSyntaxCheck = params => {
    return axios.post(`${hulkurl}/api/server/sqlJobSyntaxCheck`, params).then(res => res.data); };
export const recallSqlJob = params => {
    return axios.post(`${hulkurl}/api/server/recallDbJob`, params).then(res => res.data); };


export const getTableList = params => { return axios.get(`${hulkurl}/api/server/tableList?dbname=` + params.dbname + "&instance=" + params.instance);};

export const doSelect = params => {
    return axios.post(`${hulkurl}/api/server/execDbSelect`, params).then(res => res.data); };

export const getSlowLog = _ => { return axios.get(`${hulkurl}/api/server/slowLog`)};

//ansible-系统管理
export const getHostList = _ => { return axios.get(`${hulkurl}/api/ansible/hosts`)};
export const getIpLists = _ => { return axios.get(`${hulkurl}/api/ansible/ipLists`)};
export const updateItemHosts = params => {
    return axios.post(`${hulkurl}/api/ansible/editHostGroup`, params).then(res => res.data); };

//ansible 模块管理
export const runAnsibleModuleCmd = params => {
    return axios.post(`${hulkurl}/api/ansible/runModule`, params).then(res => res.data); };
export const runAnsiblePlaybook = params => {
    return axios.post(`${hulkurl}/api/ansible/runPlaybook`, params).then(res => res.data); };



export const AddItemHosts = params => {
    console.log("update item hosts.....",params);
    return axios.post(`${hulkurl}/api/ansible/addHostGroup`, params).then(res => res.data); };

//静态模板加载
export const LoadTemplateFile = params => {
    console.log("load file url .....",params);
    return axios.get(`${hulkurl}`+ params).then(res => res.data); };

export const GetFileSize = params => {
    console.log("load file url .....",params);
    return axios.get(`${hulkurl}/api/file/stat?filepath=`+ params.filepath).then(res => res.data); };


export const getTemplateLists = _ => { return axios.get(`${hulkurl}/api/ansible/templatePath`)};



//auto deployment
export const AddDeployItem = params => {
    return axios.post(`${hulkurl}/api/deploy/additem`, params).then(res => res.data); };

export const AddDeployView = params => {
    return axios.post(`${hulkurl}/api/deploy/addview`, params).then(res => res.data); };
export const saveDeployView = params => {
    return axios.post(`${hulkurl}/api/deploy/saveview`, params).then(res => res.data); };
export const deleteDeployView = params => {
    return axios.post(`${hulkurl}/api/deploy/deleteview`, params).then(res => res.data); };

export const stopDeploy = param => { return axios.get(`${hulkurl}/api/deploy/terminateDeploy?name=` + param.name)};


export const getDeployItems = _ => { return axios.get(`${hulkurl}/api/deploy/items`)};
export const getDeployViews = _ => { return axios.get(`${hulkurl}/api/deploy/views`)};
export const getDeployView = param => { return axios.get(`${hulkurl}/api/deploy/view?name=`+ param.name)};

export const getDeployItem = param => { return axios.get(`${hulkurl}/api/deploy/item?name=`+ param.name).then(res => res.data); };
export const getViewItems = param => { return axios.get(`${hulkurl}/api/deploy/view/items?name=`+ param.name).then(res => res.data); };

export const LoadDeployQeueus = _ => { return axios.get(`${hulkurl}/api/deploy/queues`).then(res => res.data); };
export const getDeployConcurrency = _ => { return axios.get(`${hulkurl}/api/deploy/concurrency`).then(res => res.data); };
export const updateDeployConcurrencyMaxwoker = param => { return axios.get(`${hulkurl}/api/deploy/changeConcurrency?max=`+ param.max).then(res => res.data); };


export const getDeploymentTemplateLists = _ => { return axios.get(`${hulkurl}/api/kubernetes/deployment/templatePath`)};

export const LoadWorkspaceFile = params => {
    console.log("load file url .....",params);
    return axios.get(`${hulkurl}`+ params).then(res => res.data); };

export const SaveDeployConfig = params => {
    console.log("update item hosts.....",params);
    return axios.post(`${hulkurl}/api/deploy/saveItemConfig`, params).then(res => res.data); };

export const StartDeploy = params => {
    console.log("update item hosts.....",params);
    return axios.post(`${hulkurl}/api/deploy/startDeploy`, params).then(res => res.data); };


export const DeleteItem = params => {
    console.log(params);
    return axios.post(`${hulkurl}/api/deploy/deleteItem`, params).then(res => res.data); };

export const LoadWorkspace = params => {
    return axios.get(`${hulkurl}/api/deploy/workspaces?item=`+ params.name).then(res => res.data); };

export const CleanWorkspace = params => {
    return axios.post(`${hulkurl}/api/deploy/cleanWorkspace`, params).then(res => res.data); };

export const LoadOutput = params => {
    return axios.get(`${hulkurl}/api/deploy/output?item=`+ params.name).then(res => res.data); };


export const LoadProgressData = _ => { return axios.get(`${hulkurl}/api/deploy/deployProgress`)};

export const AddDeployJob = params => {
    return axios.post(`${hulkurl}/api/deploy/addJob`, params).then(res => res.data); };




export const getDeployJobs = _ => { return axios.get(`${hulkurl}/api/deploy/jobList`)};

export const GetDeploymentJobStatus = params => {
    return axios.get(`${hulkurl}/api/deploy/deploymentJobStatus?id=`+ params.id).then(res => res.data); };


export const StartDeployJob = params => {
    return axios.post(`${hulkurl}/api/deploy/beginDeployJob`, params).then(res => res.data); };

export const CloseDeploymentJobStatus = params => {
    return axios.get(`${hulkurl}/api/deploy/closedeploymentJob?id=`+ params.id).then(res => res.data); };


export const StartDeploySqlJobByhand = params => {
    return axios.post(`${hulkurl}/api/deploy/beginDeploySqlJobByHand`, params).then(res => res.data); };

export const StartDeployItemJobByhand = params => {
    return axios.get(`${hulkurl}/api/deploy/beginDeployItemJob?id=`+ params.id + '&name=' + params.name).then(res => res.data); };

export const DeleteDeployJob = params => {
    return axios.post(`${hulkurl}/api/deploy/deleteDeployJob`, params).then(res => res.data); };


//监控

export const getMonitorNodes = _ => { return axios.get(`${hulkurl}/api/monitor/nodes`)};
export const getMonitorMysqlNodes = _ => { return axios.get(`${hulkurl}/api/monitor/mysqlNodes`)};

export const monitorHostData = params => {
    return axios.get(`${hulkurl}/api/monitor/host?ip=`+ params.ip + '&durtion=' + params.durtion).then(res => res.data); };

export const alertEntrySet = params => {
    return axios.post(`${hulkurl}/api/monitor/alertSetting`, params).then(res => res.data); };

export const monitorAlertData = params => {
    return axios.get(`${hulkurl}/api/monitor/alertStatus?ip=`+ params.ip).then(res => res.data); };


export const monitorMysqlData = params => {
    return axios.get(`${hulkurl}/api/monitor/mysql?ip=`+ params.ip + '&durtion=' + params.durtion).then(res => res.data); };

export const monitorRestData = params => {
    return axios.get(`${hulkurl}/api/monitor/interface`).then(res => res.data); };

export const monitorRestDistributionData = params => {
    return axios.post(`${hulkurl}/api/monitor/restResponseCodeDistrbution`, params).then(res => res.data); };

export const k8sNodesData = params => {
    return axios.get(`${hulkurl}/api/kubernetes/node`).then(res => res.data); };

export const k8sNodeDetailData = params => {
    return axios.get(`${hulkurl}/api/kubernetes/nodeDetail?name=` + params.name).then(res => res.data); };

export const k8sNamespacesData = params => {
    return axios.get(`${hulkurl}/api/kubernetes/namespaces`).then(res => res.data); };

export const k8sNamespaceDetailData = params => {
    return axios.get(`${hulkurl}/api/kubernetes/namespaceDetail?name=` + params.name).then(res => res.data); };


export const k8sClusterRoleData = params => {
    return axios.get(`${hulkurl}/api/kubernetes/clusterRoles`).then(res => res.data); };


export const k8sDeploymentDetailData = params => {
    return axios.get(`${hulkurl}/api/kubernetes/deployment?namespace=`+ params.namespace + '&name=' + params.name).then(res => res.data); };

export const k8sReplicaSetDetailData = params => {
    return axios.get(`${hulkurl}/api/kubernetes/replicaset?namespace=`+ params.namespace + '&name=' + params.name).then(res => res.data); };

export const k8sPodDetailData = params => {
    return axios.get(`${hulkurl}/api/kubernetes/pod?namespace=`+ params.namespace + '&name=' + params.name).then(res => res.data); };

export const k8sConfigmapData = params => {
    return axios.get(`${hulkurl}/api/kubernetes/configmap?namespace=`+ params.namespace + '&name=' + params.name).then(res => res.data); };

export const k8sSecretData = params => {
    return axios.get(`${hulkurl}/api/kubernetes/secret?namespace=`+ params.namespace + '&name=' + params.name).then(res => res.data); };

export const k8sJobDetailData = params => {
    return axios.get(`${hulkurl}/api/kubernetes/job?namespace=`+ params.namespace + '&name=' + params.name).then(res => res.data); };

export const k8sStatefulsetDetailData = params => {
    return axios.get(`${hulkurl}/api/kubernetes/statefulset?namespace=`+ params.namespace + '&name=' + params.name).then(res => res.data); };

export const k8sPersistentVolumeData = params => {
    return axios.get(`${hulkurl}/api/kubernetes/persistentvolumes`).then(res => res.data); };

export const k8sPersistentVolumeDetailData = params => {
    return axios.get(`${hulkurl}/api/kubernetes/persistentvolume?name=`+ params.name).then(res => res.data); };

export const k8sPersistentVolumeClaimDetailData = params => {
    return axios.get(`${hulkurl}/api/kubernetes/persistentvolumeclaim?namespace=`+ params.namespace + '&name=' + params.name).then(res => res.data); };

export const k8sDaemonsetDetailData = params => {
    return axios.get(`${hulkurl}/api/kubernetes/daemonset?namespace=`+ params.namespace + '&name=' + params.name).then(res => res.data); };

export const k8sLogsData = params => {
    return axios.get(`${hulkurl}/api/kubernetes/logs?namespace=`+ params.namespace + '&name=' + params.name).then(res => res.data); };

export const deleteK8sPod = params => {
    return axios.delete(`${hulkurl}/api/kubernetes/pod?namespace=`+ params.namespace + '&name=' + params.name).then(res => res.data); };

export const K8sPodYaml = params => {
    return axios.get(`${hulkurl}/api/kubernetes/_raw/pod?namespace=`+ params.namespace + '&name=' + params.name).then(res => res.data); };

export const UpdatePodUseYaml = params => {
    return axios.put(`${hulkurl}/api/kubernetes/_raw/pod?namespace=`+ params.namespace + '&name=' + params.name,params.podYaml).then(res => res.data); };

export const deleteK8sResource = params => {
    return axios.delete(`${hulkurl}/api/kubernetes/_del/resource?namespace=`+ params.namespace + '&name=' + params.name + '&type=' + params.type).then(res => res.data); };
export const K8sResourceYaml = params => {
    return axios.get(`${hulkurl}/api/kubernetes/_raw/resource?namespace=`+ params.namespace + '&name=' + params.name + '&type=' + params.type).then(res => res.data); };
export const UpdateResourceUseYaml = params => {
    return axios.put(`${hulkurl}/api/kubernetes/_raw/resource?namespace=`+ params.namespace + '&name=' + params.name + '&type=' + params.type,params.podYaml).then(res => res.data); };

